#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "cgi-lib.h"
#include "cgi-llist.h"
#include "html-lib.h"
#include "string-lib.h"

#include "tlib_all.h"

int main()
{
	TLIB_HTML_TEMPLATE stTemplate;
	TLIB_HTML_TEMPLATE_PART stTemplatePart;
	char sErrMsg[300];
	int iRetCode;
	FILE *fp;
	char *sOutputFile = "/tmp/t_tlib_html_ex.html";

	if ((fp = fopen(sOutputFile, "w+")) == NULL) 
	{
		sprintf(sErrMsg, "Fail To Open Output File:%s", sOutputFile);
		return 1;
	}

	
	iRetCode = TLib_Html_ExLoad("/", "usr/local/comm/tlib/tlib_html/example/test_ex.html", &stTemplate, sErrMsg);
	if (iRetCode != 0) 
	{
		printf("Fail To Load HTML Template File: %s\n", sErrMsg);
		return iRetCode;
	}

printf("OK1\n");

	iRetCode = TLib_Html_ExGetFirstTemplatePart(&stTemplate, &stTemplatePart, sErrMsg);
	while (1)
	{
		if (iRetCode < 0)
		{
			printf("Error: %s\n", sErrMsg);
			break;
		}
		
		if (iRetCode > 0)
		{
			break;
		}
		
printf("OK2\n");
	
		if (TLib_Html_FillTemplateFile(fp, sErrMsg, stTemplatePart.pcBuffer,
					"HEADER", stTemplatePart.pcHeader,
					NULL) != 0)
		{
			printf("Fail To Fill Template: %s", sErrMsg);
			break;
		}
		
		TLib_Html_FillTemplate(stTemplatePart.pcBuffer,
					"HEADER", stTemplatePart.pcHeader,
					NULL);
printf("OK3\n");
		TLib_Html_ExFreePart(&stTemplatePart);
printf("OK4\n");
		iRetCode = TLib_Html_ExGetNextTemplatePart(&stTemplate, &stTemplatePart, sErrMsg);
printf("OK5\n");
	}

	TLib_Html_ExFreePart(&stTemplatePart);
	TLib_Html_ExFree(&stTemplate);
	fclose(fp);
	return 0;
}
